var request = require('../../utils/request.js')
var time = require('../../utils/time.js')
var app = getApp()

Page({
  data: {
    time:'00:00:00',
    detail:'',
    show:false,
    interval:'',
    num:null,
    waitCharge: 0
  },
  enter:function(){
    my.switchTab({
        url: 'index'
    })
  },
  stop:function () {
    var that = this
     my.confirm({
      title: '提示',
      content: '确定结束充电么？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        // my.alert({
        //   title: `${result.confirm}`,
        // });
        if(result.confirm){
          let success = function() {
            // that.getchargestatus();
            my.switchTab({
              url: 'index'
            });
          }
          request.postdata('/Index/finishcharge', { num:this.data.num}, success, that)
        }else{
          return;
        }
       
      },
    });
  },
   //点击充电结束，判断当前状态，等于3则显示充电结束
  // getchargestatus: function() {
  //   let success = (data) => {
  //     clearInterval(this.data.interval);
  //     if (data.status == 3 || data.status == 4) {
  //       this.setData({
  //         show: true,
  //         detail: data,
  //         time: '00:00:00'
  //       })
  //     } else {
  //       setTimeout(() => {
  //         this.getchargestatus();
  //       }, 5000)
  //     }
  //   }
  //   request.postdata('/Index/chargeStatus', { num: this.data.num}, success, this);
  // },
  getData:function(){
    var that=this
    var success=function(data){
      that.setData({
        detail:data
      })
    }
    request.postdata('/Index/chargeStatus',{},success,that)
  },
  getStatus:function(){
    var that = this
    var success = function(data) {
      clearInterval(that.data.interval);
      if (data.status == 1) { //待充电
        that.setData({
          show: true,
          detail: data
        })
        that.data.waitCharge += 1
        if (that.data.waitCharge < 6) {
          setTimeout(() => {
            that.getStatus();
          }, 10000)
        } else {
          my.showToast({
            content: '充电失败',
            type: "fail",
            duration: 1000,
          })
        }
      } else if (data.status == 2) { //充电中
        let cTime = data.ctime.replace(/-/g, "/");
        let ctime = new Date(cTime).getTime(); //充电开始时间
        let mydate = new Date().getTime(); //当前时间
        let count = data.chargetime * 60 - Math.floor((mydate - ctime) / 1000);
        that.data.interval = setInterval(() => {
          count--;
          if (count <= 0) {
            that.setData({
              show: false
            });
            clearInterval(that.data.interval);
          }
          that.setData({
            show: true,
            time: time.countdown(count)
          });
          if (count === 0) {
            clearInterval(that.data.interval)
            that.getData()
          }
          that.setData({
          detail: data,
        });
        }, 1000)
      } else {
        that.setData({
          show: true,
          detail: data,
          time: '00:00:00'
        });
      }
    }
    request.postdata('/Index/chargeStatus', {num: this.data.num}, success, that);
  },
  onLoad:function(data){
    this.data.num=data.num
  },
  onShow:function(){
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
    this.getStatus()
  }
})
